﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Xml.Linq;

using BLL;

namespace CTRL
{
    public class CtrlMaps
    {
        private static CtrlMaps instance = null;
        private static readonly object padlock = new object();
        private static readonly string mapsFile = String.Format("{0}\\{1}", System.IO.Directory.GetCurrentDirectory(), "maps.xml");

        List<Map> mapsList;

        public CtrlMaps()
        {
            mapsList = new List<Map>();
            LoadMapsFile();
        }

        public List<Map> GetMapList()
        {
            return mapsList;
        }

        public List<Map> LoadMapsFile()
        {
            mapsList.Clear();

            var xml = XDocument.Load(mapsFile).Descendants("map").ToList();

            xml.ForEach(x => mapsList.Add(
                new Map
                {
                    Name = x.Element("name").Value.ToString(),
                    DemoID = Int32.Parse(x.Element("demo").Value.ToString()),
                    DownloadID = Int32.Parse(x.Element("download").Value.ToString())
                }
                ));

            return mapsList;
        }

        public static CtrlMaps Instance
        {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new CtrlMaps();
                    }

                    return instance;
                }
            }
        }
    }
}